package com.rjwh.dingdong.client.activity.loginregist.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.loginregist.ForgetPwdActivity;
import com.rjwh.dingdong.client.activity.loginregist.UserRegisterActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.LoginPresenter;
import com.rjwh.dingdong.client.mvp.view.LoginView;
import com.rjwh.dingdong.client.util.RxBus;
import com.rjwh.dingdong.client.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 帐号密码登录
 *
 * @author 王博扬
 * @ClassName AccountLoginFragment
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2015-11-18 上午10:52:51
 * @history 1.YYYY-MM-DD author: description:
 */
public class AccountLoginFragment extends BaseFragment implements LoginView, TextWatcher, TextView.OnEditorActionListener {

    @BindView(R.id.account_login_account)
    EditText loginNameEt;
    @BindView(R.id.account_login_password)
    EditText passwordEt;
    @BindView(R.id.account_login_btn)
    Button loginBtn;
    private String account;
    private String password;
    private LoginPresenter presenter;

    public static AccountLoginFragment getInstance() {
        AccountLoginFragment fragment = new AccountLoginFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.account_login_fragment_layout, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new LoginPresenter(getContext(), this);
    }

    private void initView() {
        loginNameEt.setText(MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_LOGIN_NAME));
//        passwordEt.setText(MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_LOGIN_PASSWORD));

        checkEditText();

        loginNameEt.addTextChangedListener(this);
        passwordEt.addTextChangedListener(this);
        passwordEt.setOnEditorActionListener(this);
    }

    private void checkEditText(){
        if (!AsStrUtils.isEmpty(loginNameEt.getText().toString()) && !AsStrUtils.isEmpty(passwordEt.getText().toString())) {
            loginBtn.setBackgroundResource(R.drawable.long_btn);
            loginBtn.setClickable(true);
        } else {
            loginBtn.setBackgroundResource(R.drawable.long_btn_unable);
            loginBtn.setClickable(false);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkEditText();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        switch (actionId) {
            case EditorInfo.IME_ACTION_DONE:
                account = loginNameEt.getText().toString();
                password = passwordEt.getText().toString();
                if (AsStrUtils.isEmpty(account)) {
                    ToastUtil.showToast(getContext(), "请输入用户名");
                    return false;
                }
                if (AsStrUtils.isEmpty(password)) {
                    ToastUtil.showToast(getContext(), "请输入密码");
                    return false;
                }
                presenter.doLogin(account, password);
                break;

            default:
                break;
        }
        return true;
    }

    @OnClick({R.id.account_login_user_register, R.id.account_login_forget_psw_tv, R.id.account_login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.account_login_btn:
                account = loginNameEt.getText().toString();
                password = passwordEt.getText().toString();
                presenter.doLogin(account, password);
                break;
            case R.id.account_login_forget_psw_tv:
                Intent regetPwd = new Intent(getContext(), ForgetPwdActivity.class);
                startActivity(regetPwd);
                break;
            case R.id.account_login_user_register:
                Intent register = new Intent(getContext(), UserRegisterActivity.class);
                startActivity(register);
                break;
        }
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void LoadData() {
        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_LOGIN_NAME, account);
//        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_LOGIN_PASSWORD, password);
        RxBus.get().post(LocalConstant.RX__POST_LOGIN_UPDATE_UI, 1);
        RxBus.get().post(LocalConstant.RX__POST_WEB_FETCHINFO, 1);
        getActivity().finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

}
